import Scroll from '@/components/mixins/scroll.js';
import MaterialApi from "@/api/user/material";
export default {
	mixins: [Scroll],
	data() {
		return {
			path: {
				parents: [],
				dir: {}
			},
			query: {
				name: '',
			},
			selection: [],
		}
	},
	methods: {
		init: async function() {
			await this.loadPath();
			this.selection = [];
			this.initialize = true;
			this.downCallback();
		},
		loadPath: async function(dir) {
			let res = await MaterialApi.path({
				id: dir != null ? dir.id : null
			});
			this.path.parents = res.obj.parents || [];
			this.path.dir = res.obj.dir || {}
		},
		loadPage: async function() {
			this.query.id = this.path.dir.id;
			let res = await MaterialApi.page(this.query, this.param);
			return res.obj || {};
		},
		onMaterialOps: function(ops) {
			if (ops && ops.type === 'select') {
				this.onSelection(ops.file);
			}
		},
		onSelection: function(file) {
			let index = this.selection.findIndex(sel => sel.id === file.id);
			if (index >= 0) {
				this.selection.splice(index, 1);
			}
			if (file.selected) {
				this.selection.push(file);
			}
		},
		bindEvent: function() {
			console.log('绑定事件：onMaterialOps')
			this.$Bus.$on('material:ops', this.onMaterialOps);
		},
		unBindEvent: function() {
			console.log('解除绑定事件：onMaterialOps')
			this.$Bus.$off('material:ops', this.onMaterialOps)
		}
	},
	onUnload() {
		this.unBindEvent();
	},
	beforeDestroy() {
		this.unBindEvent();
	},
}
